package com.patni.body.detail;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.patni.body.Body;
import com.patni.detail1.bean.Detail1Bean;
import com.patni.detail1.bean.ModuleListBean;

public class Detail1Pan {
private Detail1Bean detail1Bean;

private int num;
private JPanel detail1Pan;

private List<JPanel> panList;
private List<JPanel> categoryPan;
private List<Integer> tureHeight;
private List<String> largeString;
private List<JLabel> largeLabel;
private int height=0;
private Icon open;
private Icon close;

public final static int width=500;

public final static int titleHeight=25;
public Detail1Pan(Detail1Bean detail1Bean){
	this.detail1Bean=detail1Bean;
	panList=new ArrayList<JPanel>();
	categoryPan=new ArrayList<JPanel>();
	tureHeight=new ArrayList<Integer>();
	largeString=new ArrayList<String>();
	largeLabel=new ArrayList<JLabel>();
	
	
//	open=new JLabel();
//	close=new JLabel();
	open=new ImageIcon("bodyImage/list_open.png");
	close=new ImageIcon("bodyImage/list_close.png");
	
	setDetail1Pan(new JPanel());
	getDetail1Pan().setLayout(null);
	getDetail1Pan().setLocation(0, 0);
	getDetail1Pan().setSize(0,0);
	initPanel();

	getDetail1Pan().addMouseMotionListener(new MouseMotionListener() {
		
		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
	});
}

public void initPanel(){
	panList.removeAll(panList);
	categoryPan.removeAll(categoryPan);
	largeLabel.removeAll(largeLabel);
	num=detail1Bean.getDetail1Bean().size();
//	for(ModuleListBean i:detail1Bean.getDetail1Bean()){
////		System.out.println("((((((((((((((((((((((((((((((((((((((");
//	
//		largeString.add(i.isLarge()?"yes":"no");
//		largeLabel.add(i.isLarge()?open:close);
//			
//		JPanel p=new ModuleList(i).getModuleListPan();	
//		panList.add(p);
//		tureHeight.add(p.getHeight());
//		
//		JPanel p1=createCategoryPan(i);
//		p1.add(comp)
//		categoryPan.add(createCategoryPan(i));
//		
//		
//			
//	}
	
	for(int i=0;i<num;i++){
		ModuleListBean bean=detail1Bean.getDetail1Bean().get(i);
		
		largeString.add(bean.isLarge()?"yes":"no");
//		largeLabel.add(bean.isLarge()?open:close);
		
		JPanel p=new ModuleList(bean).getModuleListPan();	
		panList.add(p);
		tureHeight.add(p.getHeight());
		
		JPanel p1=createCategoryPan(bean);
//		JPanel p1=new CategoryPan(bean.getCategory(),bean.getMouduleBeanList().size(),"");		
//		p1.add(largeLabel.get(i));
		categoryPan.add(p1);
		
		JLabel l=new JLabel();
		l.setSize( 20, 20);
		largeLabel.add(l);
	}
	
	reSize();
	addMouseLisner();
	
}

public void addMouseLisner(){
	for(int i=0;i<num;i++){
		final int flag=i;
		categoryPan.get(i).addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				if(largeString.get(flag).equals("yes")){
					largeString.set(flag, "no");
					reSize();
				}else{
					largeString.set(flag, "yes");
					reSize();
				}
			}
		});
	}
}

public  void reSize(){
	height=0;

	System.out.println("(()()("+panList.size());
	
for(int i=0;i<num;i++){
	int y=height;
	panList.get(i).setLocation(0, y);
	categoryPan.get(i).setLocation(0,y);
	largeLabel.get(i).setLocation(0, y);
	if(largeString.get(i).equals("yes")){
		panList.get(i).setSize(width,tureHeight.get(i));
		largeLabel.get(i).setIcon(open);

	}else {
		panList.get(i).setSize(width,titleHeight);
		largeLabel.get(i).setIcon(close);

	}
	detail1Pan.add(largeLabel.get(i));
	detail1Pan.add(categoryPan.get(i));
	detail1Pan.add(panList.get(i));
	height+=panList.get(i).getHeight();
}	
 detail1Pan.setSize(width,height);
 
 System.out.println("_----__width:"+width+"  height:"+height);

 Body.setBodyHeight(width,height);
}

public JPanel createCategoryPan(ModuleListBean i){
	JPanel pan=new JPanel();
	JLabel l=new JLabel("");
	l.setText(i.getCategory()+" ["+i.getMouduleBeanList().size()+"]");
	l.setFont(new Font("宋体",Font.PLAIN,15));
	l.setForeground(Color.black);
	l.setBounds(Detail1Pan.titleHeight, 0,Detail1Pan.width-Detail1Pan.titleHeight, Detail1Pan.titleHeight);
	pan.setLayout(null);
	pan.add(l);
	pan.setSize( Detail1Pan.width, Detail1Pan.titleHeight);
	pan.setBackground(Color.white);
//	pan.add(open);
//	pan.add(close);
	return pan;
}

public void setDetail1Pan(JPanel detail1Pan) {
	this.detail1Pan = detail1Pan;
}

public JPanel getDetail1Pan() {
	return detail1Pan;
}
}

class CategoryPan extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public CategoryPan(String cateName,int num,String large){
		JPanel pan=new JPanel();
		JLabel l=new JLabel("");
		l.setText(cateName+"   ["+num+"]");
		
//		l.setForeground(Color.black);
		l.setBounds(Detail1Pan.titleHeight, 0,Detail1Pan.width-Detail1Pan.titleHeight, Detail1Pan.titleHeight);
		pan.setLayout(null);
		pan.add(l);
		pan.setSize( Detail1Pan.width, Detail1Pan.titleHeight);
		pan.setBackground(new Color(244,249,252));
	}
}